package com.cf.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cf.domain.ResponseResult;
import com.cf.domain.pojo.Team;
import com.cf.domain.pojo.TeamMember;
import com.cf.domain.vo.TeamVO;
import com.cf.domain.vo.UserVO;
import com.cf.exception.MyException;
import com.cf.service.ITeamMemberService;
import com.cf.service.ITeamService;
import com.cf.service.IUserService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequiredArgsConstructor
public class TeamController {

    private final ITeamService teamService;
    private final ITeamMemberService teamMemberService;
    private final IUserService userService;

    @RequestMapping ("/teamUI")
    public String teamUI(HttpSession session, Model model){
        UserVO user = (UserVO) session.getAttribute("user");
        Team team = teamService.getByUserId(user.getId());
        List<TeamMember> data = teamMemberService.listByTeamId(team.getId());
        team.setTeamMembers(data);
        model.addAttribute("team", team);
        return "user/team";
    }

    /**
     * 更改团队信息
     * @return
     */
    @PostMapping("/team")
    @ResponseBody
    public ResponseResult team(@RequestParam("teamName") String teamName,
                               @RequestParam("coach") Integer coach,
                               @RequestParam("plan") String plan,
                               @RequestParam("content") String content,
                               @RequestParam("res") String res,
                               HttpSession session) throws IOException {
        if(StringUtils.isBlank(plan)){
            throw new MyException("请上传项目计划书!");
        }
//        if(StringUtils.isBlank(content)){
//            throw new MyException("请上传项目介绍视频!");
//        }
        //解析团队成员集合数据
        ObjectMapper objectMapper = new ObjectMapper();
        List<Map<String, String>> data = objectMapper.readValue(res, new TypeReference<List<Map<String, String>>>() {
        });
        List<TeamMember> list = new ArrayList<>();
        for(Map<String, String> m:data){
            TeamMember t = new TeamMember();
            t.setMemberName(m.get("memberName"));
            t.setMemberPhone(m.get("memberPhone"));
            t.setMemberEmail(m.get("memberEmail"));
            list.add(t);
        }
        //判断team是否已经存在了
        UserVO user = (UserVO) session.getAttribute("user");
        Team team = teamService.getByUserId(user.getId());
        if(team.getId() == null){
            team = new Team();
            team.setTeamName(teamName);
            team.setCoach(coach);
            team.setUserId(user.getId());
            team.setPlan(plan);
            team.setVideo(content);
            team.setTeamMembers(list);
            teamService.insert(team);
        }else{
            team.setTeamName(teamName);
            team.setCoach(coach);
            team.setPlan(plan);
            team.setVideo(content);
            team.setTeamMembers(list);
            teamService.updateTeam(team);
        }

        return ResponseResult.sucess(null);
    }

    @GetMapping("/team/list")
    @ResponseBody
    public ResponseResult getList() {
        List<TeamVO> data = userService.selectMembers(null);
        System.out.println("data = " + data.toString());
        return ResponseResult.sucess(data);
    }
}
